
using System;
using System.Collections;

namespace Game.Network
{
    public delegate void EventListenerDelegate(BaseEvent evt);
    public delegate void EventListner<TEvent>(TEvent event_);
    public delegate void EventListenerIntDelegate(int v);
    public class EventDispatcher
    {
        // Fields
        private Hashtable listeners = new Hashtable();
        private object target;

        // Methods
        public EventDispatcher(object target)
        {
            this.target = target;
        }

        public void AddEventListener(string eventType, EventListenerDelegate listener)
        {
            EventListenerDelegate a = this.listeners[eventType] as EventListenerDelegate;
            a = (EventListenerDelegate) Delegate.Combine(a, listener);
            this.listeners[eventType] = a;
        }

       public void DispatchEvent(BaseEvent evt)
        {
            EventListenerDelegate delegate2 = this.listeners[evt.Type] as EventListenerDelegate;
            if (delegate2 != null)
            {
                try
                {
                    delegate2(evt);
                }
                catch (Exception exception)
                {
                    string[] textArray1 = new string[] { "Error dispatching event ", evt.Type, ": ", exception.Message, " ", exception.StackTrace };
                    throw new Exception(string.Concat(textArray1), exception);
                }
            }
        }

        public void RemoveAll()
        {
            this.listeners.Clear();
        }

        public void RemoveEventListener(string eventType, EventListenerDelegate listener)
        {
            EventListenerDelegate source = this.listeners[eventType] as EventListenerDelegate;
            if (source != null)
            {
                source = (EventListenerDelegate) Delegate.Remove(source, listener);
            }
            this.listeners[eventType] = source;
        }

        public void RemoveEventListener(string eventType) { listeners.Remove(eventType); }
    }
}

